package org.htmlparser.util;

import java.io.Serializable;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.visitors.NodeVisitor;

public class NodeList
  implements Serializable
{
  private static final int INITIAL_CAPACITY = 10;
  private int capacity;
  private int capacityIncrement;
  private Node[] nodeData;
  private int size;

  public NodeList()
  {
    removeAll();
  }

  public NodeList(Node paramNode)
  {
    add(paramNode);
  }

  static int access$000(NodeList paramNodeList)
  {
    return paramNodeList.size;
  }

  static Node[] access$100(NodeList paramNodeList)
  {
    return paramNodeList.nodeData;
  }

  private void adjustVectorCapacity()
  {
    this.capacity += this.capacityIncrement;
    this.capacityIncrement = (2 * this.capacityIncrement);
    Node[] arrayOfNode = this.nodeData;
    this.nodeData = newNodeArrayFor(this.capacity);
    System.arraycopy(arrayOfNode, 0, this.nodeData, 0, this.size);
  }

  private Node[] newNodeArrayFor(int paramInt)
  {
    return new Node[paramInt];
  }

  public void add(Node paramNode)
  {
    if (this.size == this.capacity)
      adjustVectorCapacity();
    Node[] arrayOfNode = this.nodeData;
    int i = this.size;
    this.size = (i + 1);
    arrayOfNode[i] = paramNode;
  }

  public void add(NodeList paramNodeList)
  {
    for (int i = 0; i < paramNodeList.size; ++i)
      add(paramNodeList.nodeData[i]);
  }

  public String asString()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < this.size; ++i)
      localStringBuffer.append(this.nodeData[i].toPlainTextString());
    return localStringBuffer.toString();
  }

  public boolean contains(Node paramNode)
  {
    int i;
    if (-1 != indexOf(paramNode))
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  public void copyToNodeArray(Node[] paramArrayOfNode)
  {
    System.arraycopy(this.nodeData, 0, paramArrayOfNode, 0, this.size);
  }

  public Node elementAt(int paramInt)
  {
    return this.nodeData[paramInt];
  }

  public SimpleNodeIterator elements()
  {
    return new 1(this);
  }

  public NodeList extractAllNodesThatMatch(NodeFilter paramNodeFilter)
  {
    return extractAllNodesThatMatch(paramNodeFilter, false);
  }

  public NodeList extractAllNodesThatMatch(NodeFilter paramNodeFilter, boolean paramBoolean)
  {
    NodeList localNodeList1 = new NodeList();
    for (int i = 0; i < this.size; ++i)
    {
      Node localNode = this.nodeData[i];
      if (paramNodeFilter.accept(localNode))
        localNodeList1.add(localNode);
      if (paramBoolean)
      {
        NodeList localNodeList2 = localNode.getChildren();
        if (localNodeList2 != null)
          localNodeList1.add(localNodeList2.extractAllNodesThatMatch(paramNodeFilter, paramBoolean));
      }
    }
    return localNodeList1;
  }

  public int indexOf(Node paramNode)
  {
    int i = -1;
    for (int j = 0; (j < this.size) && (-1 == i); ++j)
      if (this.nodeData[j].equals(paramNode))
        i = j;
    return i;
  }

  public void keepAllNodesThatMatch(NodeFilter paramNodeFilter)
  {
    keepAllNodesThatMatch(paramNodeFilter, false);
  }

  public void keepAllNodesThatMatch(NodeFilter paramNodeFilter, boolean paramBoolean)
  {
    int i = 0;
    while (true)
    {
      Node localNode;
      while (true)
      {
        if (i >= this.size)
          return;
        localNode = this.nodeData[i];
        if (paramNodeFilter.accept(localNode))
          break;
        remove(i);
      }
      if (paramBoolean)
      {
        NodeList localNodeList = localNode.getChildren();
        if (localNodeList != null)
          localNodeList.keepAllNodesThatMatch(paramNodeFilter, paramBoolean);
      }
      ++i;
    }
  }

  public void prepend(Node paramNode)
  {
    if (this.size == this.capacity)
      adjustVectorCapacity();
    System.arraycopy(this.nodeData, 0, this.nodeData, 1, this.size);
    this.size = (1 + this.size);
    this.nodeData[0] = paramNode;
  }

  public Node remove(int paramInt)
  {
    Node localNode = this.nodeData[paramInt];
    System.arraycopy(this.nodeData, paramInt + 1, this.nodeData, paramInt, this.size - paramInt - 1);
    this.nodeData[(this.size - 1)] = null;
    this.size -= 1;
    return localNode;
  }

  public boolean remove(Node paramNode)
  {
    int i = 0;
    int j = indexOf(paramNode);
    if (-1 != j)
    {
      remove(j);
      i = 1;
    }
    return i;
  }

  public void removeAll()
  {
    this.size = 0;
    this.capacity = 10;
    this.nodeData = newNodeArrayFor(this.capacity);
    this.capacityIncrement = (2 * this.capacity);
  }

  public int size()
  {
    return this.size;
  }

  public String toHtml()
  {
    return toHtml(false);
  }

  public String toHtml(boolean paramBoolean)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < this.size; ++i)
      localStringBuffer.append(this.nodeData[i].toHtml(paramBoolean));
    return localStringBuffer.toString();
  }

  public Node[] toNodeArray()
  {
    Node[] arrayOfNode = newNodeArrayFor(this.size);
    System.arraycopy(this.nodeData, 0, arrayOfNode, 0, this.size);
    return arrayOfNode;
  }

  public String toString()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < this.size; ++i)
      localStringBuffer.append(this.nodeData[i]);
    return localStringBuffer.toString();
  }

  public void visitAllNodesWith(NodeVisitor paramNodeVisitor)
    throws ParserException
  {
    paramNodeVisitor.beginParsing();
    for (int i = 0; i < this.size; ++i)
      this.nodeData[i].accept(paramNodeVisitor);
    paramNodeVisitor.finishedParsing();
  }
}